package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Books;
import com.example.demo.service.BooksService;
import com.example.demo.mapper.BooksMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author GJC
* @description 针对表【books】的数据库操作Service实现
* @createDate 2025-01-12 23:14:37
*/
@Service
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books>
    implements BooksService{
    //获取所有图书信息，返回图书列表。
    public List<Books> getList(){
        return  this.baseMapper.selectList(new QueryWrapper<>());
    }
    //获取单本图书信息
    public Books getBook(int id){
        QueryWrapper<Books> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return this.baseMapper.selectOne(queryWrapper);
    }
    //添加新图书
    public int addBook(Books book){
        return this.baseMapper.insert(book);
    }
    //根据图书ID更新图书信息
    public int updateBook(Books book){
        QueryWrapper<Books> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",book.getId());
        return this.baseMapper.update(book,queryWrapper);
    }
    //根据图书ID删除图书（需管理员权限）
    public int delBook(int id){
        QueryWrapper<Books> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return this.baseMapper.delete(queryWrapper);
    }
    //根据关键词返回相关图书列表。
    public List<Books> searchBook(String keyword){
        QueryWrapper<Books> queryWrapper= new QueryWrapper<>();
        queryWrapper.like("title",keyword);
        return this.baseMapper.selectList(queryWrapper);
    }
}




